<?php

namespace app\task\controller;

use app\task\logic\DealIntegralLogic;

/**
 * 处理积分（天）api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/11/28
 * Time: 17:03
 */
class DealIntegral extends Base
{
    public function __construct()
    {
        parent::__construct();
        $this->di_logic = new DealIntegralLogic();
    }

    /**
     * 处理未完成社区巡店积分
     * @param $deal_date
     * @return \think\response\Json
     */
    public function deal_no_cp_integral($deal_date)
    {
        $maps = [];
        $maps[] = ['time_code', '<>', 'other'];
        $maps[] = ['now_date', '=', $deal_date];
        $maps[] = ['holiday', '=', 0];
        $maps[] = ['status', '=', 0];
        $cbids = model('cp_branch')->get_column($maps, 'id');

        $si = new \app\common\controller\SaveIntegral();
        foreach ($cbids as $k => $v) {
            //更新未完成社区巡店积分
            $r = $si->save_no_cp_integral($v);
        }

        return json($this->di_logic->get_response(1, '处理未完成社区巡店积分，日期：' . $deal_date));
    }

    /**
     * 处理未发布推广工作积分
     * @param $deal_date
     * @return \think\response\Json
     */
    public function deal_no_re_integral($deal_date)
    {
        $ymd = date('Ymd', strtotime($deal_date));

        $h = new \app\common\controller\Holiday();
        $rh = $h->get_date_holiday($deal_date);
        if ($rh['holiday'] == 0) {
            //获取指定日期所在天的开始时间和结束时间
            $r = get_the_today_time($deal_date);
            $start_time = $r['start_time'];
            $end_time = $r['end_time'];

            //查找所有用户
            $r = get_today_time();
            $today_start_time = $r['start_time'];
            $today_end_time = $r['end_time'];
            $maps = [];
            $maps[] = ['role', '>', 1];
            $maps[] = ['c_time', 'not between', [$today_start_time, $today_end_time]];
            $u_openids = model('user')->get_column($maps, 'openid');

            //查找发布的所有用户
            $maps = [];
            $maps[] = ['c_time', 'between', [$start_time, $end_time]];
            $wre_openids = model('work_record_extend')->get_column($maps, 'openid');

            //未发布的用户
            $no_openids = array_diff($u_openids, $wre_openids);

            //查找已处理未发布的用户
            $maps = [];
            $maps[] = ['openid', 'in', $no_openids];
            $maps[] = ['sourceid', '=', $ymd];
            $maps[] = ['source', '=', 'no_release_extend'];
            $ir_openids = model('integral_record')->get_column($maps, 'openid');

            $si = new \app\common\controller\SaveIntegral();

            //需要处理的用户
            $openids = array_diff($no_openids, $ir_openids);
            foreach ($openids as $k => $v) {
                //更新未发布推广工作积分
                $r = $si->save_no_re_integral($v, $ymd);
            }
        }
        return json($this->di_logic->get_response(1, '处理未发布推广工作积分，日期：' . $deal_date));
    }

    /**
     * 处理事项超时积分
     * @return \think\response\Json
     */
    public function deal_otm_integral()
    {
        $now_time = date('Y-m-d H:i:s');

        $maps = [];
        $maps[] = ['out_time', '<>', '0000-00-00 00:00:00'];
        $maps[] = ['out_time', '<', $now_time];
        $maps[] = ['status', '=', 2];
        $list = model('matter')->get_list($maps, 'id,deal_openid');

        $si = new \app\common\controller\SaveIntegral();
        foreach ($list as $k => $v) {
            //更新超时事项积分
            $r = $si->save_otm_integral($v['deal_openid'], $v['id']);
        }
        return json($this->di_logic->get_response(1, '处理事项超时积分，日期：' . $now_time));
    }
}
